package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.TiposIntervencao;

import com.model.Manutencoes;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table MANUTENCOES.
*/
public class ManutencoesDao extends AbstractDao<Manutencoes, Long> {

    public static final String TABLENAME = "MANUTENCOES";

    /**
     * Properties of entity Manutencoes.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idManutencoes");
        public final static Property Data = new Property(1, String.class, "data", false, "DATA");
        public final static Property NomeApiario = new Property(2, String.class, "nomeApiario", false, "NOME_APIARIO");
        public final static Property Descricao = new Property(3, String.class, "Descricao", false, "DESCRICAO");
        public final static Property Ano = new Property(4, String.class, "ano", false, "ANO");
        public final static Property Isnote = new Property(5, Boolean.class, "isnote", false, "ISNOTE");
        public final static Property Updateinsert = new Property(6, Boolean.class, "updateinsert", false, "UPDATEINSERT");
        public final static Property CodApiario = new Property(7, long.class, "codApiario", false, "COD_APIARIO");
        public final static Property CodApicultor = new Property(8, long.class, "codApicultor", false, "COD_APICULTOR");
        public final static Property Tipodeaccao = new Property(9, long.class, "tipodeaccao", false, "TIPODEACCAO");
    };

    private DaoSession daoSession;

    private Query<Manutencoes> apiario_ApiarioManutencoesQuery;
    private Query<Manutencoes> apicultor_ApicultorManutencoesQuery;
    private Query<Manutencoes> tiposIntervencao_TipoIntervManutencoesQuery;

    public ManutencoesDao(DaoConfig config) {
        super(config);
    }
    
    public ManutencoesDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'MANUTENCOES' (" + //
                "'_idManutencoes' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'DATA' TEXT," + // 1: data
                "'NOME_APIARIO' TEXT," + // 2: nomeApiario
                "'DESCRICAO' TEXT," + // 3: Descricao
                "'ANO' TEXT," + // 4: ano
                "'ISNOTE' INTEGER," + // 5: isnote
                "'UPDATEINSERT' INTEGER," + // 6: updateinsert
                "'COD_APIARIO' INTEGER NOT NULL ," + // 7: codApiario
                "'COD_APICULTOR' INTEGER NOT NULL ," + // 8: codApicultor
                "'TIPODEACCAO' INTEGER NOT NULL );"); // 9: tipodeaccao
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'MANUTENCOES'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Manutencoes entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String data = entity.getData();
        if (data != null) {
            stmt.bindString(2, data);
        }
 
        String nomeApiario = entity.getNomeApiario();
        if (nomeApiario != null) {
            stmt.bindString(3, nomeApiario);
        }
 
        String Descricao = entity.getDescricao();
        if (Descricao != null) {
            stmt.bindString(4, Descricao);
        }
 
        String ano = entity.getAno();
        if (ano != null) {
            stmt.bindString(5, ano);
        }
 
        Boolean isnote = entity.getIsnote();
        if (isnote != null) {
            stmt.bindLong(6, isnote ? 1l: 0l);
        }
 
        Boolean updateinsert = entity.getUpdateinsert();
        if (updateinsert != null) {
            stmt.bindLong(7, updateinsert ? 1l: 0l);
        }
        stmt.bindLong(8, entity.getCodApiario());
        stmt.bindLong(9, entity.getCodApicultor());
        stmt.bindLong(10, entity.getTipodeaccao());
    }

    @Override
    protected void attachEntity(Manutencoes entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Manutencoes readEntity(Cursor cursor, int offset) {
        Manutencoes entity = new Manutencoes( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // data
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // nomeApiario
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // Descricao
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // ano
            cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0, // isnote
            cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0, // updateinsert
            cursor.getLong(offset + 7), // codApiario
            cursor.getLong(offset + 8), // codApicultor
            cursor.getLong(offset + 9) // tipodeaccao
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Manutencoes entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setData(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setNomeApiario(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setDescricao(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setAno(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setIsnote(cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0);
        entity.setUpdateinsert(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
        entity.setCodApiario(cursor.getLong(offset + 7));
        entity.setCodApicultor(cursor.getLong(offset + 8));
        entity.setTipodeaccao(cursor.getLong(offset + 9));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Manutencoes entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Manutencoes entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioManutencoes" to-many relationship of Apiario. */
    public List<Manutencoes> _queryApiario_ApiarioManutencoes(long codApiario) {
        synchronized (this) {
            if (apiario_ApiarioManutencoesQuery == null) {
                QueryBuilder<Manutencoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApiario.eq(null));
                apiario_ApiarioManutencoesQuery = queryBuilder.build();
            }
        }
        Query<Manutencoes> query = apiario_ApiarioManutencoesQuery.forCurrentThread();
        query.setParameter(0, codApiario);
        return query.list();
    }

    /** Internal query to resolve the "apicultorManutencoes" to-many relationship of Apicultor. */
    public List<Manutencoes> _queryApicultor_ApicultorManutencoes(long codApicultor) {
        synchronized (this) {
            if (apicultor_ApicultorManutencoesQuery == null) {
                QueryBuilder<Manutencoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApicultor.eq(null));
                apicultor_ApicultorManutencoesQuery = queryBuilder.build();
            }
        }
        Query<Manutencoes> query = apicultor_ApicultorManutencoesQuery.forCurrentThread();
        query.setParameter(0, codApicultor);
        return query.list();
    }

    /** Internal query to resolve the "tipoIntervManutencoes" to-many relationship of TiposIntervencao. */
    public List<Manutencoes> _queryTiposIntervencao_TipoIntervManutencoes(long tipodeaccao) {
        synchronized (this) {
            if (tiposIntervencao_TipoIntervManutencoesQuery == null) {
                QueryBuilder<Manutencoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Tipodeaccao.eq(null));
                tiposIntervencao_TipoIntervManutencoesQuery = queryBuilder.build();
            }
        }
        Query<Manutencoes> query = tiposIntervencao_TipoIntervManutencoesQuery.forCurrentThread();
        query.setParameter(0, tipodeaccao);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getTiposIntervencaoDao().getAllColumns());
            builder.append(" FROM MANUTENCOES T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'COD_APIARIO'=T0.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T1 ON T.'COD_APICULTOR'=T1.'_idApicultor'");
            builder.append(" LEFT JOIN TIPOS_INTERVENCAO T2 ON T.'TIPODEACCAO'=T2.'_idTiposIntervencao'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Manutencoes loadCurrentDeep(Cursor cursor, boolean lock) {
        Manutencoes entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        TiposIntervencao tiposIntervencao = loadCurrentOther(daoSession.getTiposIntervencaoDao(), cursor, offset);
         if(tiposIntervencao != null) {
            entity.setTiposIntervencao(tiposIntervencao);
        }

        return entity;    
    }

    public Manutencoes loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Manutencoes> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Manutencoes> list = new ArrayList<Manutencoes>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Manutencoes> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Manutencoes> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
